package com.lfp.controller;

import com.lfp.config.rabbitmq.DelayedQueueConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description TODO
 * @Author lfp
 * @Date 2022/7/19
 * @Version 1.0
 */
@Slf4j
@RestController
@Api(tags = "延时队列插件接口")
@RequestMapping("plugin")
public class DelayedQueuePluginController {
    @Autowired
    private RabbitTemplate rabbitTemplate;


    @ApiOperation("发送基于插件的延时消息")
    @PostMapping("sendDelayedMsg/{message}/{delayTime}")
    public void sendDelayedMsg(@PathVariable("message")String message,
                               @PathVariable("delayTime") Integer delayTime){

        rabbitTemplate.convertAndSend(
                DelayedQueueConfig.DELAYED_EXCHANGE_NAME,
                DelayedQueueConfig.DELAYED_ROUTING_KEY,
                message.getBytes(),
                message1 -> {
                    //通过Lambda表达式方式，给消息设置延迟时长，单位：ms
                    message1.getMessageProperties().setDelay(delayTime);
                    return message1;
                });

        log.info("发送一条时长为:{}毫秒的延时消息给延时队列，消息内容为：{}",delayTime,message);
    }
}
